package com.leetroll.media.job;

import com.leetroll.common.entity.SysConfig;
import com.leetroll.common.enums.EConfigKey;
import com.leetroll.common.utils.AliyunOSSUtil;
import com.leetroll.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class Job_Config {
    private static final String FILE_PATH_EFFECTS = "F:\\_dev\\leetroll\\配置资源\\bg_effects";
    private static final String FILE_PATH_FRAMES = "F:\\_dev\\leetroll\\配置资源\\frame_effects";

    public void initBgEffects() throws IOException {
        File folder = new File(FILE_PATH_EFFECTS);
        if (!folder.exists() || !folder.isDirectory()) return;

        File[] docFolders = folder.listFiles();
        if (docFolders == null) return;

        Map<String, List<String>> map = new HashMap<>();
        for (File effectFolder : docFolders) {
            uploadBgEffect(effectFolder, map);
        }

        String json = JsonUtils.objectToJson(map);
        SysConfig sysConfig = new SysConfig();
        sysConfig.setKey(EConfigKey.BG_EFFECTS.getKey());
        sysConfig.setValue(json);
        sysConfig.setId(6L);
        sysConfig.insert();

        log.info("finished: {}", map.size());
    }

    private void uploadBgEffect(File effectFolder, Map<String, List<String>> map) throws IOException {
        File[] effectFiles = effectFolder.listFiles();
        if (effectFiles == null) return;

        List<String> urls = new ArrayList<>();
        for (File effectFile : effectFiles) {
            String path = "config/bg_effects/" + effectFolder.getName() + "/" + effectFile.getName();
            AliyunOSSUtil.upload(path, Files.newInputStream(effectFile.toPath()));
            String url = AliyunOSSUtil.GetUrl(path, 10);
            urls.add(url);
        }
        map.putIfAbsent(effectFolder.getName(), urls);
    }


    public void initFramesEffect() throws IOException {
        File folder = new File(FILE_PATH_FRAMES);
        if (!folder.exists() || !folder.isDirectory()) return;

        File[] frames = folder.listFiles();
        if (frames == null) return;

        Map<String, String> map = new HashMap<>();
        for (File file : frames) {
            uploadFramesEffect(file, map);
        }

        String json = JsonUtils.objectToJson(map);
        SysConfig sysConfig = new SysConfig();
        sysConfig.setKey(EConfigKey.FRAME_EFFECTS.getKey());
        sysConfig.setValue(json);
        sysConfig.setId(8L);
        sysConfig.insert();

        log.info("finished: {}", map.size());
    }

    private void uploadFramesEffect(File frameFile, Map<String, String> map) throws IOException {
        String name = frameFile.getName().split("\\.")[0];
        String path = "config/frame_effects/" + name + "/" + frameFile.getName();
        AliyunOSSUtil.upload(path, Files.newInputStream(frameFile.toPath()));
        String url = AliyunOSSUtil.GetUrl(path, 10);
        map.putIfAbsent(name, url);
    }
}
